<?php
/*
$gtrans=new gtrans;
if ($text=$gtrans->query('Hello world','en','zh')) {
    echo $text;
} else {
    echo $gtrans->error();
}
*/
require 'httpreq.php';
class gtrans {
    private $httpreq;
    private $error;
    private $errno;
    public function __construct() {
        $this->httpreq=new httpreq;
    }  
    public function query($text, $from, $to) {
        if ($from==$to) {
            return $text;
        }
        $text=trim($text);
        if ($from=='zh' && $to=='en') {
            if ($text=='乒乓球') {
                return 'Table Tennis';  
            } else if ($text=='足球') {
                return 'Soccer';
            } else if ($text=='橄榄球') {
                return 'Football';
            }
        }
        //使\n可以保留。format参数不起作用，不知道为什么
        //只能说google translate api太beta了
        $text=str_replace('~','-',$text);
        $text=preg_replace("/\n+/",'~',$text);

        mb_internal_encoding('UTF-8');
        $len=mb_strlen($text);
        $max_len=4999;
        static $puncs=array(
            ',','，','。','.',
            '|',
            ';',
            '；',
            '！',
            '!',
            '?',
            '？',
        );
 

        $text_arr=array();
        $current_offset=0;
        while (1) {
            if ($len<$max_len) {
                $text_arr[]=mb_substr($text,$current_offset,$len);
                break;
            }

            $cut_len=$max_len;
            /*如果在最后500个字符里找到标点，则截取到该处*/
            foreach ($puncs as $punc) {
                if (($_tmp=mb_strpos($text,$punc,$current_offset+$max_len-500))!==false) {
                    $cut_len=$_tmp-$current_offset+1;
                    if ($cut_len>$max_len) {
                        $cut_len=$max_len;
                    }
                    break;
                }
            }

            $text_arr[]=mb_substr($text,$current_offset,$cut_len);
            $current_offset+=$cut_len;
            $len-=$cut_len;
        }

        $all_result='';
        foreach ($text_arr as $text) {
            $parameters=array(
                    'v'=>'1.0',
                    'langpair'=>$from.'|'.$to,
                    'q'=>$text,
                    );
            /*
               $query_string='';
               foreach ($parameters as $k=>$v) {
               $query_string.='&'.$k.'='.urlencode($v);
               }
               if (is_array($text)) {
               foreach ($text as $t) {
               $query_string.='&q='.urlencode($t);
               }
               } else {
               $query_string.='&q='.urlencode($text);
               }
               $query_string=substr($query_string,1);
             */
            //$url='http://ajax.googleapis.com/ajax/services/language/translate?'.$query_string;
            $url='http://ajax.googleapis.com/ajax/services/language/translate';
            $this->httpreq->actAs(httpreq::AGENT_FIREFOX_3_5_MAC);
            $this->httpreq->setMethod(httpreq::METHOD_POST);
            $this->httpreq->setPostData($parameters);
            $this->httpreq->setUrl($url);
            $httpres=$this->httpreq->send();
            $json_res=json_decode($httpres->body());
            if (@$json_res->responseStatus==200) {
                $str=$json_res->responseData->translatedText;
                $str=str_replace('~',"\n",$str);
                $all_result.=html_entity_decode($str,ENT_COMPAT,'UTF-8');
            } else {
                if (isset($json_res->responseStatus)) {
                    $this->errno=$json_res->responseStatus;
                    $this->error=$json_res->responseDetails;
                } else {
                    $this->errno=0;
                    $this->error='err: http status: '.$httpres->status();
                }
                return false;
            }
        }
        return $all_result;
    }
    public function error() {
        return $this->error;
    }
    public function errno() {
        return $this->errno;
    }
}
